package com.kawaiior.asoulworld.world.worldOreGen;

import com.kawaiior.asoulworld.blocks.ModBlocks;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.gen.IChunkGenerator;
import net.minecraft.world.gen.feature.WorldGenMinable;
import net.minecraftforge.fml.common.IWorldGenerator;

import java.util.Random;

public class ModWorldGen implements IWorldGenerator {
    @Override
    public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider) {
        if (world.provider.getDimension() == 0){
            generateOverworld(random, chunkX, chunkZ, world, chunkGenerator, chunkProvider);
        }
    }

    private void generateOverworld(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider){

        generateOre(ModBlocks.ASOUL_SOUL_BLOCK.getDefaultState(), world, random, chunkX*16, chunkZ*16, 5, 32, random.nextInt(12)+1,  2);

        generateOre(ModBlocks.ASOUL_FUEL_BLOCK_A.getDefaultState(), world, random, chunkX*16, chunkZ*16, 5, 32, random.nextInt(12)+1, 3);
        generateOre(ModBlocks.ASOUL_FUEL_BLOCK_B.getDefaultState(), world, random, chunkX*16, chunkZ*16, 5, 32, random.nextInt(12)+1, 3);
        generateOre(ModBlocks.ASOUL_FUEL_BLOCK_C.getDefaultState(), world, random, chunkX*16, chunkZ*16, 5, 32, random.nextInt(12)+1, 3);
        generateOre(ModBlocks.ASOUL_FUEL_BLOCK_D.getDefaultState(), world, random, chunkX*16, chunkZ*16, 5, 32, random.nextInt(12)+1, 3);
        generateOre(ModBlocks.ASOUL_FUEL_BLOCK_E.getDefaultState(), world, random, chunkX*16, chunkZ*16, 5, 32, random.nextInt(12)+1, 3);
    }

    private void generateOre(IBlockState ore, World world, Random random, int x, int z, int minY, int maxY, int size, int chances){
        int deltaY = maxY - minY;

        for (int i = 0; i < chances; i++) {
            BlockPos pos = new BlockPos(x+random.nextInt(16), minY+random.nextInt(deltaY), z+random.nextInt(16));

            WorldGenMinable genMinable = new WorldGenMinable(ore, size);
            genMinable.generate(world, random, pos);
        }
    }
}
